package com.example.lets;

import android.accounts.AccountAuthenticatorActivity;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bmob.BmobPro;
import com.bmob.BmobProFile;
import com.bmob.btp.callback.DownloadListener;
import com.bmob.btp.callback.LocalThumbnailListener;
import com.gc.materialdesign.widgets.SnackBar;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;

class MyAdapter extends BaseAdapter {

    private LayoutInflater mInflater;
    private ArrayList<HashMap<String, Object>> list;
    private Context context;

    public MyAdapter(Context context, ArrayList<HashMap<String, Object>> list) {
        this.mInflater = LayoutInflater.from(context);
        this.list = list;
        this.context = context;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        final ViewHolder holder;
        Log.v("MyListViewBase", "getView " + position + " " + convertView);
        if (convertView == null) {

            convertView = mInflater.inflate(R.layout.listview_item, null);
            holder = new ViewHolder();

            holder.title    = (TextView) convertView.findViewById(R.id.ItemTitle);
            holder.sponsor  = (TextView) convertView.findViewById(R.id.ItemSponsor);
            holder.date     = (TextView) convertView.findViewById(R.id.ItemDate);
            holder.location = (TextView) convertView.findViewById(R.id.ItemLocation);
            holder.number   = (TextView) convertView.findViewById(R.id.ItemPeopleNumber);
            holder.image    = (ImageView) convertView.findViewById(R.id.ItemImage);
            holder.distance = (TextView) convertView.findViewById(R.id.distance);

            convertView.setTag(holder);
        } else holder = (ViewHolder) convertView.getTag();

        holder.title.setText(list.get(position).get("ItemTitle").toString());
        holder.sponsor.setText("BY " + list.get(position).get("ItemSponsor").toString());
        holder.date.setText(list.get(position).get("time").toString());
        holder.location.setText(list.get(position).get("place").toString() + " "
                              + list.get(position).get("detail").toString());
        holder.distance.setText(list.get(position).get("distance").toString());
        holder.number.setText("人数" + list.get(position).get("number").toString());

        holder.sponsor.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(context, "打开用户页面", Toast.LENGTH_SHORT).show();
            }
        });

        holder.number.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(context, "打开已加入用户列表", Toast.LENGTH_SHORT).show();
            }
        });

        String fileName = (String) list.get(position).get("ItemPicFileName");
        String cacheDownloadPath = BmobPro.getInstance(context).getCacheDownloadDir() + "/" + fileName;
        String[] name = fileName.split("\\.");
        String newFileName = name[0] + "_1_200_0_100." + name[1];
        String cacheThumbnailPath = BmobPro.getInstance(context).getCacheThumbnailDir() + "/" + newFileName;

        if ((new File(cacheDownloadPath)).exists()) {

            if ((new File(cacheThumbnailPath)).exists()) {

                Bitmap bm = BitmapFactory.decodeFile(cacheThumbnailPath);
                holder.image.setImageBitmap(bm);

            } else {

                BmobProFile.getInstance(context).getLocalThumbnail(cacheDownloadPath, 1, new LocalThumbnailListener() {

                    @Override
                    public void onSuccess(String thumbnailPath) {
                        Log.i("bmob", "localThumbnail--> :" + thumbnailPath);
                        Bitmap bm = BitmapFactory.decodeFile(thumbnailPath);
                        holder.image.setImageBitmap(bm);
                    }

                    @Override
                    public void onError(int statuscode, String errormsg) {
                        Log.i("bmob", "localThumbnail--> :" + statuscode + "," + errormsg);
                    }
                });

            }

        } else {

            BmobProFile.getInstance(context).download(fileName, new DownloadListener() {

                @Override
                public void onSuccess(String fullPath) {
                    Log.i("bmob", "���سɹ���" + fullPath);
                    BmobProFile.getInstance(context).getLocalThumbnail(fullPath, 1, new LocalThumbnailListener() {

                        @Override
                        public void onSuccess(String thumbnailPath) {
                            Log.i("bmob", "localThumbnail-->���ɺ������ͼ·�� :" + thumbnailPath);
                            Bitmap bm = BitmapFactory.decodeFile(thumbnailPath);
                            holder.image.setImageBitmap(bm);
                        }

                        @Override
                        public void onError(int statuscode, String errormsg) {
                            Log.i("bmob", "localThumbnail-->��������ͼʧ�� :" + statuscode + "," + errormsg);
                        }
                    });
                    String str = BmobPro.getInstance(context).getCacheDownloadDir();
                    Log.i("bmob", str);
                }

                @Override
                public void onProgress(String localPath, int percent) {
                    Log.i("bmob", "download-->onProgress :" + percent);
                }

                @Override
                public void onError(int statuscode, String errormsg) {
                    Log.i("bmob", "���س���" + statuscode + "--" + errormsg);
                }
            });
        }
        return convertView;
    }
}

class ViewHolder {
    public ImageView image;
    public TextView title;
    public TextView sponsor;
    public TextView date;
    public TextView location;
    public TextView number;
    public TextView distance;
}